package com.opensource.bykc.controller;


import com.opensource.bykc.entity.GPSPoint;
import com.opensource.bykc.entity.GPSPointDto;
import com.opensource.bykc.entity.PageDto;
import com.opensource.bykc.result.AppHttpCodeEnum;
import com.opensource.bykc.result.ResponseResult;
import com.opensource.bykc.service.GPSPointService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author wzh
 * @date 2024/5/24
 * @apiNote
 */
@CrossOrigin
@RestController
@RequestMapping
public class controller {

    @Autowired
    private GPSPointService service;


    @PostMapping
    public ResponseResult save(@RequestParam("shape") String lonlat, String name) {
        boolean flag = service.add(lonlat, name);

        return flag ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }


    @GetMapping
    public ResponseResult find(String start, String end) {
        List<String> shapes = service.find(start, end);

        return ResponseResult.okResult(shapes);
    }

    @GetMapping("/page")
    public ResponseResult findpage(@RequestParam(required = false) String start,
                                   @RequestParam(required = false) String end,
                                   @RequestParam(defaultValue = "1") Integer curPage,
                                   @RequestParam(defaultValue = "5") Integer pageSize
    ) {
        PageDto data = service.findPage(start, end, curPage, pageSize);

        return ResponseResult.okResult(data);
    }

    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Integer id) {
        boolean flag = service.removeById(id);
        return flag ? ResponseResult.okResult("删除成功") : ResponseResult.errorResult(501, "删除失败");
    }

    @PostMapping("/edit")
    public ResponseResult edit(@RequestBody GPSPointDto gpsPointDto) {
        boolean flag = service.edit(gpsPointDto);
        return flag ? ResponseResult.okResult("编辑成功") : ResponseResult.errorResult(501, "编辑失败");
    }

    @GetMapping("/demo")
    public ResponseResult demo() {
        return ResponseResult.okResult("qq");
    }

    @GetMapping("/in")
    public ResponseResult in(String shape){
        return ResponseResult.okResult(service.getIn());
    }
}
